package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.ProjectInfo;
import ywpt.model.ProjectPeople;
import ywpt.service.config.ProjectInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ProjectInfoAction extends BaseAction implements ModelDriven<ProjectInfo>, Preparable{

	/**
	 * 公司项目Action
	 */
	private static final long serialVersionUID = 3076498250632868439L;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	protected static int DEFAULT_PAGE_SIZE	=100;
	private Long id;
	private ProjectInfo projectInfo;
	private List<ProjectInfo> projectInfoList = new ArrayList<ProjectInfo>();
	private ProjectInfoService  projectInfoService;
	private int version;
	
	private Long typeId;
	private Long stateId;
	private Long departId;
	private Long peopleId;
	private String peopleName;
	private Long clientId;
	private String clientName;
	private Long keshiId;
	private String keshiName;
	private Long linkManId;
	private String linkManName;
	
	private String[] peoId;
	
	//查询相关
	private String qProjectName;
	private Long qTypeId;
	private Long qDepartId;
	private String qClentName;
	private Long qStateId;
	
	
	
	public String[] getPeoId() {
		return peoId;
	}

	public void setPeoId(String[] peoId) {
		this.peoId = peoId;
	}

	public String getPeopleName() {
		return peopleName;
	}

	public void setPeopleName(String peopleName) {
		this.peopleName = peopleName;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public String getKeshiName() {
		return keshiName;
	}

	public void setKeshiName(String keshiName) {
		this.keshiName = keshiName;
	}

	public Long getLinkManId() {
		return linkManId;
	}

	public void setLinkManId(Long linkManId) {
		this.linkManId = linkManId;
	}

	public Long getStateId() {
		return stateId;
	}

	public void setStateId(Long stateId) {
		this.stateId = stateId;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}

	public List<ProjectInfo> getProjectInfoList() {
		return projectInfoList;
	}

	public void setProjectInfoList(List<ProjectInfo> projectInfoList) {
		this.projectInfoList = projectInfoList;
	}

	public ProjectInfoService getProjectInfoService() {
		return projectInfoService;
	}

	public void setProjectInfoService(ProjectInfoService projectInfoService) {
		this.projectInfoService = projectInfoService;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public Long getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public Long getKeshiId() {
		return keshiId;
	}

	public void setKeshiId(Long keshiId) {
		this.keshiId = keshiId;
	}
	
	public String getQProjectName() {
		return qProjectName;
	}

	public void setQProjectName(String projectName) {
		qProjectName = projectName;
	}

	public Long getQTypeId() {
		return qTypeId;
	}

	public void setQTypeId(Long typeId) {
		qTypeId = typeId;
	}

	public Long getQDepartId() {
		return qDepartId;
	}

	public void setQDepartId(Long departId) {
		qDepartId = departId;
	}

	public String getQClentName() {
		return qClentName;
	}

	public void setQClentName(String clentName) {
		qClentName = clentName;
	}

	public Long getQStateId() {
		return qStateId;
	}

	public void setQStateId(Long stateId) {
		qStateId = stateId;
	}
/*-----------------------------------------------------------------------------------------------------*/
	@Override
	public ProjectInfo getModel() {
		return this.projectInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.projectInfo = this.projectInfoService.getProjectInfoById(id);
		}else{
			this.projectInfo = new ProjectInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_PROJECT);
	}
	
	protected void showInfo() {
		if(this.projectInfo.getProjectType()!=null)
			this.typeId = this.projectInfo.getProjectType().getId();
		if(this.projectInfo.getProjectState()!=null)
			this.stateId = this.projectInfo.getProjectState().getId();
		if(this.projectInfo.getDepart()!=null)
			this.departId = this.projectInfo.getDepart().getId();
		if(this.projectInfo.getResponser()!=null){
			this.peopleId = this.projectInfo.getResponser().getId();
			this.peopleName = this.projectInfo.getResponser().getPeopleName();			
		}
		if(this.projectInfo.getClient()!=null){
			this.clientId = this.projectInfo.getClient().getId();
			this.clientName = this.projectInfo.getClient().getClientName();			
		}
		if(this.projectInfo.getKeshi()!=null){
			this.keshiId = this.projectInfo.getKeshi().getId();
			this.keshiName = this.projectInfo.getKeshi().getName();
		}
		if(this.projectInfo.getLinkman()!=null){
			this.linkManId = this.projectInfo.getLinkman().getId();
			this.linkManName=this.projectInfo.getLinkman().getName();
		}
			
		getLimit();
	}
	
	protected void getLimit(){
		@SuppressWarnings("unused")		
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", this.projectInfo.getProjectScans().size(), DEFAULT_PAGE_SIZE);
	}
	
	protected void getDateList(DetachedCriteria query ){
		List<ProjectInfo> result = new ArrayList<ProjectInfo>();
		query.add(Restrictions.eq("deleteFlag", false));
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= projectInfoService.getProjectInfoCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,DEFAULT_PAGE_SIZE,0);
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		result.addAll(analyzeData(projectInfoService.getProjectInfos(query, pageSize, rowStartEnd[0])));
		projectInfoList.addAll(result);		
		limit = RequestUtils.getLimit(getRequest(),"ec",result.size(),DEFAULT_PAGE_SIZE);
	}
	
	protected List<ProjectInfo> analyzeData(List<ProjectInfo> list) {
		List<ProjectInfo> result = new ArrayList<ProjectInfo>();
		String currId ="," + this.getCurrentUser().getPeople().getId().toString()+",";
			for (ProjectInfo projectInfo:list) {
				String peoId = ",";
				peoId = peoId+projectInfo.getResponser().getId()+","+projectInfo.getPeople();
				if (projectInfo.getProjectPeoples()!=null) {
					for (ProjectPeople proPeo:projectInfo.getProjectPeoples()) {
						peoId = peoId+proPeo.getPeoStr();
					}
				}
				if (peoId.indexOf(currId)!=-1) {
					result.add(projectInfo);
				}
			}
		return result;
	}
	/*===========================================================================================*/	
	public String list() throws Exception {
		DetachedCriteria query = DetachedCriteria.forClass(ProjectInfo.class);
		getDateList(query);
		return SUCCESS;
	}
	
	public String input(){
		this.departId = this.getCurrentUser().getPeople().getDepart().getId();
		return INPUT;
	}
	
	public String edit(){
		projectInfo = projectInfoService.getProjectInfoById(id);
		showInfo();
		return INPUT;
	}
	
	public String save() throws ServletException
	{
		try
		{
			if(id==null)
			{   projectInfo.setCreater(this.getCurrentUser());
				projectInfo.setCreateDate(new Date());
				projectInfoService.saveProjectInfo(projectInfo,typeId,stateId,departId,peopleId,clientId,keshiId,linkManId,peoId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			}
			else
			{
				if(this.getVersion()==projectInfo.getVersion())
				{
					if (projectInfo.getResponser()!=null&&projectInfo.getResponser().getId().equals(this.getCurrentUser().getPeople().getId())) {
						projectInfo.setModifyer(this.getCurrentUser());
						projectInfo.setModifyDate(new Date());
						projectInfoService.updateProjectInfo(projectInfo,typeId,stateId,departId,peopleId,clientId,keshiId,linkManId,peoId);
						this.addActionMessage(SystemMessage.UPDATE_SUCCESS);						
					} else {
						this.addActionMessage("你不是该项目的负责人，不能进行信息的更新！");
					}
				}
				else
					this.addActionMessage(SystemMessage.UPDATE_FAIL);
			}			
		}	
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL+e.getMessage());
			return SUCCESS;
		}
		showInfo();
		return SUCCESS;
	}

	public String delete()
	{
		if(id!=null)
		{
			try
			{
				projectInfo = this.projectInfoService.getProjectInfoById(id);
				if (projectInfo.getResponser()!=null&&projectInfo.getResponser().getId().equals(this.getCurrentUser().getPeople().getId())) {
					projectInfoService.removeProjectInfo(id);
				} else {
					this.addActionMessage("你不是该项目的负责人，不能进行信息的更新！");
					return INPUT;
				}
			}
			catch(UniqueConstraintException e)
			{
				this.addActionMessage("删除失败！"+e.getMessage());
				return INPUT;
			}
		}
		else
		{
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
		return SUCCESS;
	}

	public String search() {
		DetachedCriteria query = DetachedCriteria.forClass(ProjectInfo.class);
			if (!StringTools.isNullOrSpace(this.qProjectName)) {
				query.add(Restrictions.like("projectName", "%"+this.qProjectName+"%"));
			}
			if (!StringTools.isNullOrSpace(this.qClentName)) {
				query.createAlias("client", "client").add(Restrictions.like("client.clientName", "%"+this.qClentName+"%"));
			} 
			if (this.qTypeId!=null) {
				query.createAlias("projectType", "projectType").add(Restrictions.eq("projectType.id", this.qTypeId));
			}
			if (this.qDepartId!=null) {
				query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", this.qDepartId));
			}
			if (this.qStateId!=null) {
				query.createAlias("projectState", "projectState").add(Restrictions.eq("projectState.id", this.qStateId));
			}
		getDateList(query);
		return SUCCESS;
	}

	public String getLinkManName() {
		return linkManName;
	}

	public void setLinkManName(String linkManName) {
		this.linkManName = linkManName;
	}


	
}
